package org.example.web.controller;

import org.example.common.api.Response;
import org.example.dao.model.User;
import org.example.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/users")
    public Response<List<User>> getUsers() {
        return Response.success(userService.selectAll());
    }

    @GetMapping("/user/{id}")
    public Response<User> getUserById(@PathVariable int id) {
        return Response.success(userService.selectById(id));
    }

    @GetMapping("/user/name/{name}")
    public Response<List<User>> getUserByName(@PathVariable String name) {
        return Response.success(userService.getUserByName(name));
    }

    @PostMapping("/user")
    public Response<Integer> addUser(@RequestBody User user) {
        return  Response.success(userService.addUser(user));
    }
}
